package com.qf.openapi.web.master.interceptor;

import com.qf.openapi.web.master.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.debug("请求进入拦截器了,请求的url={}",request.getRequestURI());
        //判断用户的登入状态 ,约定,如果session中的Key user,并且Value不为null代表浏览器曾经是登入过
        final HttpSession session = request.getSession();
        final Object user = session.getAttribute(Constants.USER_LOGIN_SESSION_KEY);
        if(user!=null){
            log.debug("请求被放行了");
            //登入过,放行
            return true;
        }
        log.debug("请求被拦截了,重定向到 登入页");
        //让浏览器重定向到 /login.html
        response.sendRedirect("/login.html");
        return false;
    }
}
